//
//  StaffManagerVC.m
//  New_Runway
//
//  Created by mack on 2018/2/7.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "StaffManagerVC.h"
#import "UserModel.h"
#import "StaffManagerCell.h"
#import "UserManagerHeader.h"
#import "StaffEditMessageVC.h"

@interface StaffManagerVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *userArray;

@end

@implementation StaffManagerVC
-(NSMutableArray *)userArray{
    if (!_userArray) {
        _userArray = [[NSMutableArray alloc] init];
    }
    return _userArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"员工管理";
    [self.navigationBarView.rightItem setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
    self.view.backgroundColor = kColorWhite();
    [self initTableview];
    [self loadStaffList];
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        
        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = MAIN_COLOR();
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"StaffManagerCell" bundle:nil] forCellReuseIdentifier:@"StaffManagerCell"];
    
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_offset(self.navH);
        make.bottom.equalTo(weakSelf.view).mas_equalTo(-self.BotH);
        make.left.right.equalTo(weakSelf.view);
    }];
    
    
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.userArray.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    StaffManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"StaffManagerCell"];
    
    UserModel *model = self.userArray[indexPath.row];
    [cell configData:model indexPath:indexPath];
    return cell;
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UserManagerHeader *view = [[[NSBundle mainBundle] loadNibNamed:@"UserManagerHeader" owner:self options:nil]lastObject];
    view.backgroundColor = UIColorHex(0xf5f5f5);
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   
    return 30;
}
- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    NSIndexPath *index = userInfo[@"index"];
    kSelfWeak;
    UserModel *model = self.userArray[index.row];
    StaffEditMessageVC *editVc = [[StaffEditMessageVC alloc] init];
    editVc.block = ^{
        [weakSelf loadStaffList];
    };
    editVc.userModel = model;
    if ([eventName isEqualToString:@"edit"]) {
        editVc.mainTitle = @"编辑员工信息";
    }else if ([eventName isEqualToString:@"look"]){
        
        editVc.mainTitle = @"查看员工信息";
    }
    [self.navigationController pushViewController:editVc animated:YES];

}
-(void)loadStaffList{
    
    NSString *url = [NSString stringWithFormat:@"%@%@",Api,staffInfoApi];
    NSDictionary *params = @{@"shopId":[NRWCarUser sharedUser].shopId ? [NRWCarUser sharedUser].shopId : @"",
                             @"marketId":[NRWCarUser sharedUser].marketId
                             };
    [SVProgressHUD show];
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:params progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        
        if ([response[@"code"] isEqual:@1]) {
            NSArray *array = response[@"result"];
            [self.userArray removeAllObjects];
            for (NSDictionary *dic  in array) {
                UserModel *model = [[UserModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.userArray addObject:model];
            }
            [SVProgressHUD dismiss];
            [self.tableview reloadData];
        }
       
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void)rightItemClick{
    kSelfWeak;
    StaffEditMessageVC *editVc = [[StaffEditMessageVC alloc] init];
    editVc.mainTitle = @"添加员工";
    editVc.block = ^{
        [weakSelf loadStaffList];
    };
    [self.navigationController pushViewController:editVc animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
